package com.dianping.cat.consumer.top.model.transform;

import com.dianping.cat.consumer.top.model.IVisitor;
import com.dianping.cat.consumer.top.model.entity.Domain;
import com.dianping.cat.consumer.top.model.entity.Error;
import com.dianping.cat.consumer.top.model.entity.Machine;
import com.dianping.cat.consumer.top.model.entity.Segment;
import com.dianping.cat.consumer.top.model.entity.TopReport;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

/*  22 */   public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
/*  26 */     this.m_out = new DataOutputStream(out);
/*  27 */     this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(TopReport topReport) {
    ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

/*  33 */     build(topReport, out);
/*  34 */     return out.toByteArray();
  }


/*  38 */   public static void build(TopReport topReport, OutputStream out) { topReport.accept(new DefaultNativeBuilder(out)); }



  public void visitDomain(Domain domain) {
/*  43 */     if (domain.getName() != null) {
/*  44 */       writeTag(1, 1);
/*  45 */       writeString(domain.getName());
    }

/*  48 */     if (!domain.getSegments().isEmpty()) {
/*  49 */       writeTag(33, 2);
/*  50 */       writeInt(domain.getSegments().size());

/*  52 */       for (Segment segment : domain.getSegments().values()) {
/*  53 */         segment.accept(this.m_visitor);
      }
    }

/*  57 */     writeTag(63, 3);
  }


  public void visitError(Error error) {
/*  62 */     if (error.getId() != null) {
/*  63 */       writeTag(1, 1);
/*  64 */       writeString(error.getId());
    }

/*  67 */     writeTag(2, 0);
/*  68 */     writeInt(error.getCount());

/*  70 */     writeTag(63, 3);
  }


  public void visitMachine(Machine machine) {
/*  75 */     if (machine.getId() != null) {
/*  76 */       writeTag(1, 1);
/*  77 */       writeString(machine.getId());
    }

/*  80 */     writeTag(2, 0);
/*  81 */     writeInt(machine.getCount());

/*  83 */     writeTag(63, 3);
  }


  public void visitSegment(Segment segment) {
/*  88 */     if (segment.getId() != null) {
/*  89 */       writeTag(1, 1);
/*  90 */       writeInt(segment.getId().intValue());
    }

/*  93 */     writeTag(2, 0);
/*  94 */     writeLong(segment.getError());

/*  96 */     writeTag(3, 0);
/*  97 */     writeLong(segment.getUrl());

/*  99 */     writeTag(4, 0);
/* 100 */     writeDouble(segment.getUrlDuration());

/* 102 */     writeTag(5, 0);
/* 103 */     writeLong(segment.getService());

/* 105 */     writeTag(6, 0);
/* 106 */     writeDouble(segment.getServiceDuration());

/* 108 */     writeTag(7, 0);
/* 109 */     writeLong(segment.getSql());

/* 111 */     writeTag(8, 0);
/* 112 */     writeDouble(segment.getSqlDuration());

/* 114 */     writeTag(9, 0);
/* 115 */     writeLong(segment.getCall());

/* 117 */     writeTag(10, 0);
/* 118 */     writeDouble(segment.getCallDuration());

/* 120 */     writeTag(11, 0);
/* 121 */     writeLong(segment.getCache());

/* 123 */     writeTag(12, 0);
/* 124 */     writeDouble(segment.getCacheDuration());

/* 126 */     writeTag(13, 0);
/* 127 */     writeLong(segment.getUrlError());

/* 129 */     writeTag(14, 0);
/* 130 */     writeDouble(segment.getUrlSum());

/* 132 */     writeTag(15, 0);
/* 133 */     writeLong(segment.getServiceError());

/* 135 */     writeTag(16, 0);
/* 136 */     writeDouble(segment.getServiceSum());

/* 138 */     writeTag(17, 0);
/* 139 */     writeLong(segment.getSqlError());

/* 141 */     writeTag(18, 0);
/* 142 */     writeDouble(segment.getSqlSum());

/* 144 */     writeTag(19, 0);
/* 145 */     writeLong(segment.getCallError());

/* 147 */     writeTag(20, 0);
/* 148 */     writeDouble(segment.getCallSum());

/* 150 */     writeTag(21, 0);
/* 151 */     writeLong(segment.getCacheError());

/* 153 */     writeTag(22, 0);
/* 154 */     writeDouble(segment.getCacheSum());

/* 156 */     if (!segment.getErrors().isEmpty()) {
/* 157 */       writeTag(33, 2);
/* 158 */       writeInt(segment.getErrors().size());

/* 160 */       for (Error error : segment.getErrors().values()) {
/* 161 */         error.accept(this.m_visitor);
      }
    }

/* 165 */     if (!segment.getMachines().isEmpty()) {
/* 166 */       writeTag(34, 2);
/* 167 */       writeInt(segment.getMachines().size());

/* 169 */       for (Machine machine : segment.getMachines().values()) {
/* 170 */         machine.accept(this.m_visitor);
      }
    }

/* 174 */     writeTag(63, 3);
  }


  public void visitTopReport(TopReport topReport) {
/* 179 */     writeTag(63, 0);

/* 181 */     if (topReport.getDomain() != null) {
/* 182 */       writeTag(1, 1);
/* 183 */       writeString(topReport.getDomain());
    }

/* 186 */     if (topReport.getStartTime() != null) {
/* 187 */       writeTag(2, 1);
/* 188 */       writeDate(topReport.getStartTime());
    }

/* 191 */     if (topReport.getEndTime() != null) {
/* 192 */       writeTag(3, 1);
/* 193 */       writeDate(topReport.getEndTime());
    }

/* 196 */     if (!topReport.getDomains().isEmpty()) {
/* 197 */       writeTag(33, 2);
/* 198 */       writeInt(topReport.getDomains().size());

/* 200 */       for (Domain domain : topReport.getDomains().values()) {
/* 201 */         domain.accept(this.m_visitor);
      }
    }

/* 205 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 210 */       writeVarint(value.getTime());
/* 211 */     } catch (Exception e) {
/* 212 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 218 */       writeVarint(Double.doubleToLongBits(value));
/* 219 */     } catch (Exception e) {
/* 220 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 226 */       writeVarint(value & 0xFFFFFFFFL);
/* 227 */     } catch (Exception e) {
/* 228 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 234 */       writeVarint(value);
/* 235 */     } catch (Exception e) {
/* 236 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 242 */       this.m_out.writeUTF(value);
/* 243 */     } catch (IOException e) {
/* 244 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 250 */       this.m_out.writeByte((field << 2) + type);
/* 251 */     } catch (IOException e) {
/* 252 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 258 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 259 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 262 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 263 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/top/model/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */